<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2018/12/12
 * Time: 17:01
 */
namespace app\common\exception;
use think\exception\Handle;

class ExceptionHandler extends Handle {
    //默认状态码
    public $status = 500;
    //默认code码
    public $code   = 500;
    //默认返回信息
    public $msg    = 'sorry, we make a mistake.';

    /**
     * rewrite render.
     * @param \Exception $e
     * @return \think\Response|\think\response\Json
     */
    public function render(\Exception $e) {
        //如果这个传入的异常类是我们自定义的异常类的话,就说明这个异常在我们的控制之中
        if ($e instanceof BaseException) {
            //将该异常设定好的属性给赋值到总的异常处理类
            $this->status = $e->status;
            $this->code   = $e->code;
            $this->msg    = $e->msg;
        } else {
            //判断配置中的debug是否开启确定开发或生产模式
            if (config('app_debug')) {
                //开发模式返回明确错误信息
                $this->msg = sprintf("%s:%s:%s", $e->getFile(), $e->getLine(), $e->getMessage());
            }
        }
        return error($this->code, $this->msg, $this->status);
    }
}